package com.bdqn.accountbook;

import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {
    private TextView content;           //账单内容
    private EditText num1;             //数字1
    private EditText num2;              //类数字2
    private RadioGroup radio;           //选择运算符
    private TextView time;              //当前时间
    private TextView orompt;            //错误提示
    private StringBuilder sb = new StringBuilder("");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        content = findViewById(R.id.content);
        num1 = findViewById(R.id.num1);
        num2 = findViewById(R.id.num2);
        radio = findViewById(R.id.radioGroup);
        time = findViewById(R.id.time);
        orompt = findViewById(R.id.orompt);
        yangqi();
    }

    public void yangqi() {
        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 1) {
                    Date day = new Date();
                    SimpleDateFormat df = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
                    time.setText(df.format(day).toString());
                    sendEmptyMessageDelayed(1, 1000);
                }
            }
        };
        handler.sendEmptyMessage(1);
    }

    /**
     * 单击重置的事件
     *
     * @param view
     */
    public void reset(View view) {
        sb.setLength(0);
        content.setText("");
    }

    /**
     * 单击添加事件
     *
     * @param view
     */
    public void add(View view) {
        if ("".equals(num1.getText().toString()) || "".equals(num2.getText().toString())) {
            orompt.setText("请输入数字");
            return;
        }
        Float n1 = Float.parseFloat(num1.getText().toString());
        Float n2 = Float.parseFloat(num2.getText().toString());

        String operator = "";
        for (int i = 0; i < radio.getChildCount(); i++) {
            RadioButton r = (RadioButton) radio.getChildAt(i);
            if (r.isChecked()) {
                operator = r.getText().toString();
            }
        }
        if ("".equals(operator)) {
            orompt.setText("请选择运算符");
            return;
        }
        if ("÷".equals(operator)) {
            if (n2 == 0) {
                orompt.setText("被除数不能为0");
                return;
            }
        }
        orompt.setText("");
        float resoult = 0f;
        switch (operator) {
            case "+":
                resoult = n1 + n2;
                break;
            case "-":
                resoult = (new BigDecimal(Float.toString(n1))).subtract((new BigDecimal(Float.toString(n2)))).floatValue();
                break;
            case "*":
                resoult = n1 * n2;
                break;
            case "÷":
                resoult = n1 / n2;
                break;
            default:
                break;
        }

        System.out.println(resoult);
        sb.append(doubleFormat(n1) + operator + doubleFormat(n2) + "=" + doubleFormat(resoult) + "\n");
        content.setText(sb.toString());
    }

    public String doubleFormat(float num) {

        System.out.println(num);
        if(num % 1.0 == 0){
            return String.valueOf((long)num);
        }
        return String.valueOf(num);
    }

}
